/**
 * created at 31.10.2010, 19:42:55
 */
package de.jmda.web.client.ping;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;


/**
 * @author rowe.jmda@gmail.com
 */
public class Ping
    extends Composite
{
	private class PingAsyncCallback implements AsyncCallback<Long>
	{
    @Override
    public void onFailure(Throwable t)
    {
			System.err.println("server error\n" + t);
    }

    @Override
    public void onSuccess(Long result)
    {
    	// create timestamp
    	Date responseDate = new Date();
			
			// store timestamp as long
			responseTime = responseDate.getTime();
			
			// update ui
			lblServerTimeValue.setText(dateTimeFormat.format(new Date(result)));
			lblResponseTimeValue.setText(dateTimeFormat.format(responseDate));
			lblDurationValue.setText((responseTime - requestTime) + " msec");
    }
	}

	private class PingClickHandler implements ClickHandler
	{
    @Override
    public void onClick(ClickEvent event)
    {
			// create timestamp
			Date requestDate = new Date();

			// store timestamp as long
			requestTime = requestDate.getTime();
			
			// call server
			pingService.ping(pingAsyncCallback);

			// update ui
			lblRequestTimeValue.setText(dateTimeFormat.format(requestDate));
    }
	}

	private Label lblRequestTimeValue;
	private Label lblResponseTimeValue;
	private Label lblServerTimeValue;
	private Label lblDurationValue;
	
	private PingServiceAsync pingService = GWT.create(PingService.class);
	private DateTimeFormat dateTimeFormat =
//			DateTimeFormat.getFormat("yyyy.MM.dd HH:mm:ss:SSS");
			DateTimeFormat.getFormat("m:ss:SSS");
	private long requestTime;
	private long responseTime;
	
	private PingAsyncCallback pingAsyncCallback = new PingAsyncCallback();

	public Ping()
	{
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setSize("78px", "109px");
		initWidget(verticalPanel);
		
		FlexTable flexTable = new FlexTable();
		verticalPanel.add(flexTable);
		flexTable.setSize("78px", "95px");
		
		Label lblHeader = new Label("evt");
		flexTable.setWidget(0, 0, lblHeader);
		
		Button btnButton = new Button("button");
		flexTable.setWidget(0, 1, btnButton);
		btnButton.addClickHandler(new PingClickHandler());
		btnButton.setText("ping");
		btnButton.setSize("50", "26px");
		
		Label lblRequestTime = new Label("req");
		flexTable.setWidget(1, 0, lblRequestTime);
		
		lblRequestTimeValue = new Label("");
		flexTable.setWidget(1, 1, lblRequestTimeValue);
		lblRequestTimeValue.setWidth("50");
		
		Label lblServerTime = new Label("srv");
		flexTable.setWidget(2, 0, lblServerTime);
		
		lblServerTimeValue = new Label("");
		flexTable.setWidget(2, 1, lblServerTimeValue);
		lblServerTimeValue.setWidth("50");
		
		Label lblResponseTime = new Label("res");
		flexTable.setWidget(3, 0, lblResponseTime);
		
		lblResponseTimeValue = new Label("");
		flexTable.setWidget(3, 1, lblResponseTimeValue);
		lblResponseTimeValue.setWidth("50");
		
		Label lblDuration = new Label("dur");
		flexTable.setWidget(4, 0, lblDuration);
		
		lblDurationValue = new Label("");
		flexTable.setWidget(4, 1, lblDurationValue);
		flexTable.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
	}
}